
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Gobang {
    public static final String WHITE_CHESS="○";
    public static final String BLACK_CHESS="●";
    //在内部修改棋盘大小
    private static int BOARD_SIZE = 15;
    private static int play = 1;
    private static int abc = 1;
    private static int index = 0;
    List<String> list=new ArrayList<>();
    //二维数组充当棋盘
    private String[][] board;
    //初始化棋盘数组
    public void initBoard()
    {
        board = new String[BOARD_SIZE][BOARD_SIZE];

        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j]="+";
            }
        }
    }

    //绘制输出棋盘
    public void printBoard()
    {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {

                System.out.print(board[i][j]);
                System.out.print("   ");
            }

            System.out.print("\n");
        }
    }


    //判断是否超出棋盘范围
    public void isOutRange(int x,int y) throws Exception
    {
        if(x>BOARD_SIZE||y>BOARD_SIZE||x<0||y<0)
        {
            throw new Exception();
        }
    }

    //判断是否重复下棋
    public void isRepetition(int x,int y) throws Exception
    {
        if(this.board[x-1][y-1]!="+")
        {
            throw new Exception();
        }
    }

    public void listremove(){
        for (int i = 0; i < list.size(); i++) {
                list.remove(i);
        }
    }

    //复盘
    public void isxiaqi1(){
        initBoard();
        int a = 0;
        while (a<index) {
            try {
                Thread.currentThread().sleep(3000);

            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            int xPos = 0;   //旗子的x坐标
            int yPos = 0;   //旗子的y坐标
            //输十个数变成10
            //将用户输入的字符串以逗号作为分隔符

            String[] posString = list.get(a++).split(",");
            //将两个字符串转换成用户下棋的坐标
            xPos = Integer.parseInt(posString[0]);
            yPos = Integer.parseInt(posString[1]);
            //把对应的数组元素赋值为○或●
            if (play< 0) {
                board[xPos - 1][yPos - 1] = BLACK_CHESS;

            } else {
                board[xPos - 1][yPos - 1] = WHITE_CHESS;

            }
            //判断游戏是否结束

            //对方准备下棋
            play = -play;
            System.out.println("---------------第"+a+"步------------------");
            printBoard();
            System.out.println("--------------------------------------");
        }
        System.out.println("游戏结束请选择重新开始");
    }


    //下棋
    public void isxiaqi(String inputStr){
        //判断黑方或者白方下棋
        //play=1，黑方下棋
        //play=-1，白方下棋
        int xPos=0;   //旗子的x坐标
        int yPos=0;   //旗子的y坐标
        index++;//输十个数变成10
        //将用户输入的字符串以逗号作为分隔符
        try{
            String[] posString = inputStr.split(",");
            //将两个字符串转换成用户下棋的坐标
            xPos = Integer.parseInt(posString[0]);
            yPos = Integer.parseInt(posString[1]);
            list.add(inputStr);
            //判断是否超出棋盘范围
            isOutRange(xPos,yPos);

            //判断是否重复下棋
            isRepetition(xPos,yPos);

            //把对应的数组元素赋值为○或●
            if(play>0)
            {
                board[xPos-1][yPos-1]=BLACK_CHESS;

            }else{
                board[xPos-1][yPos-1]=WHITE_CHESS;

            }
            //判断游戏是否结束

            //对方准备下棋
            play=-play;


        }catch (Exception e){
            System.out.println("输入的坐标不符合标准，重新输入！");
        }

        printBoard();
        if(isGameOver(xPos,yPos))//如果游戏结束
        {
            abc=0;
            if (play<0)
            {
                System.out.println("黑方获胜！");

            }else {
                System.out.println("白方获胜！");

            }
            System.out.println("选择复盘或者输入exit退出游戏");
        }

    }


    //判断游戏是否结束
    public boolean isGameOver(int x,int y)
    {
        int x1=x-1;
        int y1=y-1;
        boolean[] flag={false,false,false,false,false};//建立flag数组

        try{
            //从该下棋位置向上判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(x1-i<0)
                {
                    continue;
                }
                if(board[x1-i][y1].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向下判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(x1+i>14)
                {
                    continue;
                }
                if(board[x1+i][y1].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向左判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(y1-i<0)
                {
                    continue;
                }
                if(board[x1][y1-i].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向右判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(y1+i>14)
                {
                    continue;
                }
                if(board[x1][y1+i].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向左上方判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(x1-i<0||y1-i<0)
                {
                    continue;
                }
                if (board[x1-i][y1-i].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向右上方判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(x1-i<0||y1+i>14)
                {
                    continue;
                }
                if(board[x1-i][y1+i].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向左下方判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(x1+i>14||y1-i<0)
                {
                    continue;
                }
                if(board[x1+i][y1-i].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
            //从该下棋位置向右下方判断是否五子连珠
            for (int i = 0; i < 5; i++) {
                if(x1+i>14||y1+i>14){
                    continue;
                }
                if(board[x1+i][y1+i].equals(board[x1][y1]))
                {
                    flag[i]=true;
                }
            }
            if(flag[0]&&flag[1]&&flag[2]&&flag[3]&&flag[4])
            {
                return true;
            }
        }catch (Exception e)
        {
            return false;
        }
        return false;
    }


    public static void main(String[] args) throws Exception
    {
        //提示
        //重新开始--画棋盘
        //退出--exit
        //复盘--

        Gobang gb = new Gobang();
        gb.initBoard();
        gb.printBoard();


        Scanner scanner=new Scanner(System.in);
        String key="";
        boolean loop=true;
        while (loop){
            if (index==BOARD_SIZE*BOARD_SIZE){
                System.out.println("平局，游戏结束");
            }
            System.out.println("start:重新开始");
            System.out.println("exit:退出程序");
            System.out.println("复盘");
            if(abc!=0){
                if(play>0)
                {
                    System.out.println(">>>黑方下棋，请输入下棋的坐标，格式为x，y：");
                }else{
                    System.out.println(">>>白方下棋，请输入下棋的坐标，格式为x，y：");
                }}

            key=scanner.next();//接收一个字符
            switch (key){
                case "start":
                    gb.listremove();
                    gb.initBoard();
                    gb.printBoard();
                    break;
                case "复盘":
                    gb.isxiaqi1();
                    break;

                case "exit" :
                    scanner.close();
                    loop=false;
                    break;
                default:
                    gb.isxiaqi(key);
                    break;
            }

        }

    }
}
